(function(window, undefined){
  'use strict';
  var ns = window.beneath = window.beneath || {};

  function dist (x, y, z, w) {
    var dx = (x - z);
    var dy = (y - w);
    var l = dx * dx + dy * dy;
    return Math.sqrt(l);

  }
  function Coal(game, x, y) {
    this.index = _.sample([0,1,2,3]);
    var spr = this.spr = game.add.sprite(x, y, 'sml', this.index);
    spr.anchor.x = 0.5;
    spr.anchor.y = 0.5;


    spr.texture.baseTexture.scaleMode = 1;

    var a = Math.random() * 2 * Math.PI;
    var l = Math.random();
    var dx = Math.cos(a) * l;
    var dy = Math.sin(a) * l;
    

    spr.entity = this;
    this.spr = spr;
    
    this.near = function (x, y, r) {
      var d = dist(x, y, spr.x + 8, spr.y + 9);
      if(d <= 5 + r) {
        return true;
      }
    };
    this.update = function() {
      spr.x += dx;
      spr.y += dy;
      
      dx *= 0.95;
      dy *= 0.95;
    };
  }
  ns.Coal = Coal;

})(window);